import React, { Component, PropTypes } from "react";
import { messages } from "share/common";
import { Button, message } from 'antd'
import borrowService from "../../borrow-and-return.service";
import ExpenseCheckResult from "../../../expense-report/components/expense-check-result";
import Designate from "components/template/designate";
import requestService from "../../../request/request.service";
import errorMessage from "share/errorMessage";

class BtnSubmit extends Component {

  state = {
    loading: false,
    checkResult: [],
    showCheckResult: false,
    designateVisible: false,
    nodeList: []
  };

  handleSubmit = () => {
    const { info, close } = this.context;
    this.setState({loading: true});
    borrowService.handleSubmit(JSON.parse(JSON.stringify(info))).then(res => {
      this.setState({loading: false});
      if(Array.isArray(res.data)){
        this.setState({
          checkResult: res.data,
          showCheckResult: true
        })
      } else {
        message.success(messages('common.operate.success')/*操作成功*/);
        close(true);
      }
    }).catch(() => {
      this.setState({loading: false});
    })
  };

  submitBefore = async () => {
    const { form, info } = this.context;
    try {
      let isDesignate = await requestService.getIsDesignate({
        formOID: form.formOID,
        applicantOID: info.applicant.userOID
      });
      if (isDesignate.data && isDesignate.data.showDesignate) {
        this.setState({ loading: true });
        requestService.getNodeList({
          entityOID: info.loanBillOID,
          entityType: 3001
        }).then(result => {
          let nodeList = result.data.approvalDesignateNodeList || [];
          if (nodeList.length) {
            this.setState({
              designateVisible: true,
              nodeList
            });
          } else {
            this.handleSubmit();
          }
        }).catch(err => {
          this.setState({ loading: false });
          errorMessage(err.response);
        });
      } else {
        this.handleSubmit();
      }
    } catch (e) {
      message.error(e.response.data.message)
    }
  };

  handleCloseDesignate = isSuccess => {
    this.setState({ designateVisible: false });
    if (isSuccess) {
      this.handleSubmit();
    } else {
      this.setState({ loading: false });
    }
  };

  render() {
    const { getInfo, info } = this.context;
    const { loading, showCheckResult, checkResult, designateVisible, nodeList } = this.state;
    return (
      <span>
        <Button
          type="primary"
          onClick={this.submitBefore}
          loading={loading}
        >
          {messages('common.submit')/*提交*/}
        </Button>
        <ExpenseCheckResult
          visible={showCheckResult}
          checkResult={checkResult}
          confirmText={messages('finance-1.key106')/*继续提交*/}
          onCancel={() => {
            this.setState({ showCheckResult: false }, getInfo())
          }}
        />
        <Designate
          entity={{
            entityType: 3001,
            entityOID: info.loanBillOID
          }}
          modalVisible={designateVisible}
          nodeList={nodeList}
          onClose={this.handleCloseDesignate}
        />
      </span>
    )
  }

  static contextTypes = {
    info: PropTypes.object,
    getInfo: PropTypes.func,
    close: PropTypes.func,
    form: PropTypes.object
  }

}

export default BtnSubmit
